package com.company.gas.controller;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Li Guangdi
 *
 */
@Controller
@Api(tags = "首页相关接口")
@RequestMapping("/gas")
public class ViewController {
    @RequestMapping("/myIndex")
    public String Index(){
        return "myIndex";
    }

    @RequestMapping("/facility/add")
    public String Add(){
        return "facility/add";
    }
    @RequestMapping("/myWeishouquan")
    public String Weishouquan(){
        return "myWeishouquan";
    }

    @RequestMapping("/toLogin")
    public String ToLogin(){
        return "myLogin";
    }

    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/gas/myIndex";
    }

    //shiro实现登录逻辑验证
    @PostMapping("/myLogin")
    @ResponseBody
    public String Login(String name, String password){
        System.out.println("name = "+name);
        JSONObject jsonObject = new JSONObject();
        //使用shiro编写认证操作
        //1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token1 = new UsernamePasswordToken(name,password);
        //3.执行登陆方法
        try {
            subject.login(token1);
            //登录成功
            jsonObject.put("token1", subject.getSession().getId());
            jsonObject.put("msg", "登录成功");
        }catch (UnknownAccountException e) {
            //e.printStackTrace();
            //登陆失败: 用户名不存在
            jsonObject.put("msg", "用户名不存在");
        } catch (IncorrectCredentialsException e) {
            //e.printStackTrace();
            //登陆失败: 密码错误
            jsonObject.put("msg", "密码错误");
        }
        return jsonObject.toString();
    }
    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     * @return
     */
    @RequestMapping(value = "/unauth")
    @ResponseBody
    public Object unauth() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "1000");
        map.put("msg", "未登录");
        return map;
    }
    @RequestMapping(value = "/unshouquan",method = RequestMethod.GET)
    @ResponseBody
    public Object unshouquan() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "1001");
        map.put("msg", "未授权");
        return map;
    }

}
